//
//  ModuleVCAViewController.m
//  CocoaSynth
//
//  Created by Thomas Dupont on 22/02/10.
//  Copyright 2010 Apple Inc. All rights reserved.
//

#import "ModuleVCAViewController.h"

#import "ModuleVCAView.h"
#import "ModuleView.h"
#import "PortOutView.h"
#import "PortInView.h"
#import "MixingViewController.h"
#import "PortViewController.h"
#import "PortInViewController.h"
#import "PortOutViewController.h"
#import "PortOutProtocol.h"
#import "Module.h"
#import "ParametersModule.h"
#import "ModuleVCAProtocol.h"
#import "ParametersVCA.h"

#define VCAWIDTH 120
#define VCAHEIGHT 100
#define PORTDECALAGE 10

@implementation ModuleVCAViewController

- (id)init
{
	if (self = [super init]) {
		ModuleView* moduleView = [[ModuleVCAView alloc] initWithFrame:NSMakeRect(0, 0, VCAWIDTH, VCAHEIGHT)];
		[moduleView setController:self];
		self.view = moduleView;
		
		parameters = [[ParametersVCA alloc] initWithNibName:@"ParametersVCA" bundle:nil];
		[parameters setModule:self];
		[parameters load];
		
		[moduleView release];
	}
	return self;
}

- (void)setModel:(Module *)aModule
{
	[super setModel:aModule];
	
	NSPoint point;
	
	point.x = 10;
	point.y = VCAHEIGHT / 3 + VCAHEIGHT / 3 - PORTDECALAGE;
	PortViewController* portIn = [[PortInViewController alloc] initWithPortIn:[model portIn] atPoint:point];
	[portIn setModule:self];
	[self.view addSubview:portIn.view];
	[portsIn addObject:portIn];
	[portIn release];
	
	point.y = VCAHEIGHT / 3 - PORTDECALAGE;
	PortViewController* portControl = [[PortInViewController alloc] initWithPortIn:[(id<ModuleVCAProtocol>)model portInAm] atPoint:point];
	[portControl setModule:self];
	[self.view addSubview:portControl.view];
	[portsIn addObject:portControl];
	[portControl release];
	
	NSMutableArray* tmpPortsOut = [model portsOut];
	NSInteger nbPortsOut = [tmpPortsOut count];
	point.x = VCAWIDTH - 10 - PORTDECALAGE * 2;
	int i = 0;
	for (id<PortOutProtocol> p in tmpPortsOut){
		point.y = (VCAHEIGHT / (nbPortsOut))/2 + i * (VCAHEIGHT / (nbPortsOut)) - PORTDECALAGE;
		i++;
		PortViewController* portOut = [[PortOutViewController alloc] initWithPortOut:p atPoint:point];
		[portOut setModule:self];
		[self.view addSubview:portOut.view];
		[portsOut addObject:portOut];
		[portOut release];
	}
}

- (void)displayParameterView
{
	[mixingController displayParameterView:self];
}

- (NSViewController<ParametersModule>*)parameters
{
	return parameters;
}

- (void)highlight
{
	[(ModuleVCAView*)self.view setHighlight:YES];
	[self.view setNeedsDisplay:YES];
}

- (void)unhighlight
{
	[(ModuleVCAView*)self.view setHighlight:NO];
	[self.view setNeedsDisplay:YES];
}

- (void)dealloc
{
	[parameters release];
	[super dealloc];
}

@end
